package geo.campus;

import java.io.InputStream;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class Recherche {
	
	private double lonarr;
	private double latarr;
	private String addrarr = "";
	private String nomarr = "";
	private Spinner zone;
	private Spinner cat;
	private Spinner nom;
	private Button valider;
	private ArrayAdapter<String> adapt;
	private boolean iti;
	private Context act;
	private Dialog dialogrec;

	private DatabaseAdaptateur mabd;

	private boolean selectcat = false;
	private boolean selectzone = false;
	private boolean fini = false;

	private InputStream is;

	/** Called when the activity is first created. */
	public Recherche(boolean it, Context ctx)
	{
		iti = it;
		act = ctx;
		is = ctx.getResources().openRawResource(R.raw.install);
		adapt = new ArrayAdapter<String>(act, android.R.layout.simple_spinner_item);
		this.mabd = new DatabaseAdaptateur(act);

		mabd.open();

		String[] statements = null;
		try{
			statements = FileHelper.parseSqlFile(is);

			for (String statement : statements) {
				mabd.insertsql(statement);
			}
			is.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void recdialog(boolean it)
	{
		iti = it;
		
		dialogrec = new Dialog(act);
		dialogrec.setContentView(R.layout.recherche);
		
		valider = (Button) dialogrec.findViewById(R.id.btrecherche);
		zone = (Spinner) dialogrec.findViewById(R.id.zone);
		cat = (Spinner) dialogrec.findViewById(R.id.categorie);
		nom = (Spinner) dialogrec.findViewById(R.id.nombat);

		remplirspinnernom();
		remplirspinnercat();
		remplirspinnerzone();
		if(iti)
			dialogrec.setTitle("Selection Batiment");
		else
			dialogrec.setTitle("Recherche");
		Button annuler = (Button) dialogrec.findViewById(R.id.btrechercheann);
		
		annuler.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialogrec.cancel();
				setFini(true);
				mabd.close();
			}
		});

		valider.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(nom.getSelectedItem().toString() != "--Nom")
				{
					Cursor res = mabd.selectbynom(nom.getSelectedItem().toString());
					if(res.getCount() != 0)
					{
						res.moveToFirst();
						setNomarr(nom.getSelectedItem().toString());
						setAddrarr(res.getString(0));
						setLatarr(res.getDouble(1));
						setLonarr(res.getDouble(2));
					}
					dialogrec.cancel();
					mabd.close();
				}
				else
				{
					AlertDialog.Builder builder = new AlertDialog.Builder(act);
					builder.setMessage("Selectionner un batiment")
					.setCancelable(false)
					.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});

					AlertDialog alert = builder.create();
					alert.show();
				}
			}
		});

		zone.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(arg2 == 0)
				{
					selectzone = false;
				}
				else
				{
					if(selectcat)
					{
						Cursor c = mabd.selectbycatandid(cat.getSelectedItem().toString(), zone.getSelectedItem().toString());
						if(c.getCount() !=0)
						{
							c.moveToFirst();
							String[] res1 = new String[c.getCount()+1];
							int i = 0;
							res1[i] = "--Nom";
							do
							{
								i++;
								res1[i] = c.getString(0);
							}
							while(c.moveToNext());
							modifspinner(res1);
							c.close();
						}
						else
						{
							String[] res1 = new String[1];
							res1[0] = "--Nom";
							modifspinner(res1);
						}
					}
					else
					{
						String z = zone.getSelectedItem().toString();
						Cursor c = mabd.selectbyzone(z);
						if(c.getCount() !=0)
						{
							c.moveToFirst();
							String[] res1 = new String[c.getCount()+1];
							int i = 0;
							res1[i] = "--Nom";
							do
							{
								i++;
								res1[i] = c.getString(0);
							}
							while(c.moveToNext());
							modifspinner(res1);
							c.close();
							selectzone = true;
						}
						else
						{
							String[] res1 = new String[1];
							res1[0] = "--Nom";
							modifspinner(res1);
							selectzone = true;
						}
					}
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});

		cat.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(arg2 == 0)
				{
					selectcat = false;
				}
				else
				{
					if(selectzone)
					{
						Cursor c = mabd.selectbycatandid(cat.getSelectedItem().toString(), zone.getSelectedItem().toString());
						if(c.getCount() !=0)
						{
							c.moveToFirst();
							String[] res1 = new String[c.getCount()+1];
							int i = 0;
							res1[i] = "--Nom";
							do
							{
								i++;
								res1[i] = c.getString(0);
							}
							while(c.moveToNext());
							modifspinner(res1);
							c.close();
						}
						else
						{
							String[] res1 = new String[1];
							res1[0] = "--Nom";
							modifspinner(res1);
						}
					}
					else
					{
						Cursor c = mabd.selectbycat(cat.getSelectedItem().toString());
						if(c.getCount() !=0)
						{
							c.moveToFirst();
							String[] res1 = new String[c.getCount()+1];
							int i = 0;
							res1[i] = "--Nom";
							do
							{
								i++;
								res1[i] = c.getString(0);
							}
							while(c.moveToNext());
							modifspinner(res1);
							c.close();
							selectcat = true;
						}
						else
						{
							String[] res1 = new String[1];
							res1[0] = "--Nom";
							modifspinner(res1);
						}
					}
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		dialogrec.show();
	}
	
	private void remplirspinnernom() {
		// TODO Auto-generated method stub
		Cursor c = mabd.selectallbat();

		String[] res = new String[c.getCount()+1];

		if(c.getCount() != 0)
		{
			c.moveToFirst();
			Integer i = 0;
			res[i] = "--Nom";
			do
			{
				i++;
				res[i] = c.getString(0);
			}
			while(c.moveToNext());
		}
		for(int i = 0; i < res.length; i++)
			adapt.add(res[i]);
		adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		nom.setAdapter(adapt);
		c.close();
	}

	private void remplirspinnercat()
	{
		Cursor c1 = mabd.selectallcat();

		String[] res1 = new String[c1.getCount()+1];

		if(c1.getCount() != 0)
		{
			c1.moveToFirst();
			Integer i = 0;
			res1[i] = "--Categorie";
			do
			{
				i++;
				res1[i] = c1.getString(0);
			}
			while(c1.moveToNext());
		}
		ArrayAdapter<String> adapt1 = new ArrayAdapter<String>(act, android.R.layout.simple_spinner_item, res1);
		adapt1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		cat.setAdapter(adapt1);
		c1.close();
	}

	private void remplirspinnerzone()
	{
		Cursor c2 = mabd.selectallzone();

		String[] res2 = new String[c2.getCount()+1];

		if(c2.getCount() != 0)
		{
			c2.moveToFirst();
			Integer i = 0;
			res2[i] = "--Zone";
			do
			{
				i++;
				res2[i] = c2.getString(0);
			}
			while(c2.moveToNext());
		}
		ArrayAdapter<String> adapt2 = new ArrayAdapter<String>(act, android.R.layout.simple_spinner_item, res2);
		adapt2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		zone.setAdapter(adapt2);
		c2.close();
	}


	public InputStream getFic()
	{
		return is;
	}


	public void modifspinner(String[] res)
	{
		if(adapt.getCount() > 0)
		{
			adapt.clear();
		}

		for(int i1 = 0; i1 < res.length; i1++)
		{
			adapt.add(res[i1]);
		}

		adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		nom.setAdapter(adapt);
	}

	public double getLonarr() {
		return lonarr;
	}

	public void setLonarr(double lonarr) {
		this.lonarr = lonarr;
	}

	public double getLatarr() {
		return latarr;
	}

	public void setLatarr(double latarr) {
		this.latarr = latarr;
	}

	public String getAddrarr() {
		return addrarr;
	}

	public void setAddrarr(String addrarr) {
		this.addrarr = addrarr;
	}

	public String getNomarr() {
		return nomarr;
	}

	public void setNomarr(String nomarr) {
		this.nomarr = nomarr;
	}

	public boolean isFini() {
		return fini;
	}

	public void setFini(boolean fini) {
		this.fini = fini;
	}



}
